package com.liy.cloud.base.rest;


import java.util.List;

import com.liy.cloud.common.ext.service.IExtBaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.das.framework.common.annotation.MethodPermission;
import com.das.framework.common.extvo.ResultVO;
import com.das.framework.common.vo.SupportVO;
import com.das.framework.common.web.controller.BaseCrudController;

/**
 * @Auther: libs
 * @Date: 2018/6/13 13:53
 * @Description:
 */
public class ExtBaseController<Biz extends IExtBaseService, Entity extends SupportVO<String>> extends BaseCrudController<Biz, Entity> {

    @Autowired
    protected Biz extBaseService;

    /**
     *
     * 功能描述: 批量删除
     *
     * @param:
     * @return: 
     * @auther: libs
     * @date: 2018/6/25 19:24
     */
    @RequestMapping(
            value = {"/delete/batch"},
            method = {RequestMethod.DELETE}
    )
    @ResponseBody
    @MethodPermission(
            codes = {"DELETE"},
            description = "批量删除"
    )
    public ResultVO deleteBatch(@RequestBody List<String> ids){
        try {
            if(ids == null || ids.isEmpty()){
                return new ResultVO(false,"必须输入要删除数据的编号");
            }
            int rows = extBaseService.deleteBatch(ids);
            return new ResultVO(true,"本次删除了 " + rows + " 条数据");
        }catch (Exception e){
            log.error(e.getMessage(), e);
            return new ResultVO(false, "批量删除时发生错误:" + e.getMessage(), "500");
        }
    }

    /**
     *
     * 功能描述: 添加/修改数据时检查不能重复的字段
     *
     * @param:
     * @return:
     * @auther: libs
     * @date: 2018/6/22 13:44
     */
    @RequestMapping(
            value = {"/checkAddEdit"},
            method = {RequestMethod.GET, RequestMethod.POST}
    )
    @ResponseBody
    @MethodPermission(
            codes = {"SEARCH"},
            description = "查询"
    )
    public ResultVO checkAddEdit(Entity entity){
        ResultVO resultVO = new ResultVO();
        try {
            resultVO.setData(extBaseService.checkAddEdit(entity));
        } catch (Exception e){
            log.error(e.getMessage(), e);
            resultVO.setSuccess(false, "500");
            resultVO.setMessage("检查时发生错误:" + e.getMessage());
        }

        return resultVO;
    }

}
